package com.daryl.jcu_project1_creditscoreform;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class Display_form_loan_structure_and_purpose extends Activity {

	private EditText etTotalLoanAmount;
	private EditText etVariedAccountNo;
	private EditText etChangeToCurrentLimit;
	private EditText etLoanLimitPerSplit1;
	
	private String vTotalLoanAmount;
	private String vLoanTypes;
	private String vVariedAccountNo;
	private String vChangeToCurrentLimit;
	private String vLoanLimitPerSplit1;
	private String vLoanPurpose1;
	
	private RadioGroup rgLoanType;
	private RadioGroup rgLoanPurpose1;
	
	
	String[] selection = new String[]{"Add New Split", "Proceed To Next Page", "Cancel"};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_display_form_loan_structure_and_purpose);

		Button button_next = (Button)findViewById(R.id.button_next);
		final Context context = this;

		button_next.setOnClickListener(new View.OnClickListener(){
			public void onClick(View v){

				AlertDialog alert;
				AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);

				alertBuilder.setTitle("What would you like to do? ");
				alertBuilder.setItems(selection, new DialogInterface.OnClickListener() {
					
					public void onClick(DialogInterface dialog, int which) {
						if (which == 0){
							Intent intent = new Intent (getApplicationContext(), Display_form_loan_structure_and_purpose.class);
							startActivity(intent);
						}else if(which == 1){
							Intent intent = new Intent (getApplicationContext(), Loan_application6.class);
							startActivity(intent);
						}else{
							dialog.cancel();
						}
					}
				});
				alert = alertBuilder.create();
				alert.show();

			}
		});
		
		getRadioBtnLoanType();
		
		Display_form_fund_statement df = new Display_form_fund_statement();
		
		if(df.nextpage> 0 ) {
			setTotalLoanAmount();
			setRadioBtnLoanType();
			setVariedAccountNo();
			setChangeToCurrentLimit();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(
				R.menu.activity_display_form_loan_structure_and_purpose, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	public void nextPage (View view){
		Intent intent_next = new Intent (this, Display_form_loan_structure_and_purpose.class);
		startActivity(intent_next);
		methods md = new methods();
		 int retriveValue = 0;
		 getTotalLoanAmount();
		 getRadioBtnLoanType();
		 
		 if(md.retriveSharePreference(this, "vLoanTypes", "dLoanTypes").equals("New")){
			 getVariedAccountNoNull();
			 getChangeToCurrentLimitNull();
		 }else{
			 getVariedAccountNo();
			 getChangeToCurrentLimit();
		 }
		
	}

	public void previousPage (View view){
		Intent intent_previous = new Intent (this, Display_form_fund_statement.class);
		startActivity(intent_previous);
		
		methods md = new methods();
		md.editorShareIntPreferences(this, "previousPage4", 1);
		
		getTotalLoanAmount();
		getRadioBtnLoanType();
		
		if(md.retriveSharePreference(this, "vLoanTypes", "dLoanTypes").equals("New")){
			 getVariedAccountNoNull();
		 }else{
			 getVariedAccountNo();
		 }
	}

	public void newSelected(View view){
		LinearLayout ll1 = (LinearLayout)findViewById(R.id.llVariedAccountNumber);
		ll1.setVisibility(View.GONE);

		LinearLayout ll2 = (LinearLayout)findViewById(R.id.llChangeToCurrentLimit);
		ll2.setVisibility(View.GONE);
	}

	public void varyExistingLoanSelected(View view){
		LinearLayout ll1 = (LinearLayout)findViewById(R.id.llVariedAccountNumber);
		ll1.setVisibility(View.VISIBLE);

		LinearLayout ll2 = (LinearLayout)findViewById(R.id.llChangeToCurrentLimit);
		ll2.setVisibility(View.VISIBLE);
	}

	public void purchasePropertySelected(View view){
		TextView tv1 = (TextView)findViewById(R.id.tvReason);
		tv1.setVisibility(View.VISIBLE);

		TextView tv2 = (TextView)findViewById(R.id.tvOtherReason);
		tv2.setVisibility(View.GONE);

		RadioGroup rg1 = (RadioGroup)findViewById(R.id.rgReason);
		rg1.setVisibility(View.VISIBLE);

		TextView tv3 = (TextView)findViewById(R.id.tvLoanPurposeOther);
		tv3.setVisibility(View.GONE);

		EditText et = (EditText)findViewById(R.id.etloanPurposeOther);
		et.setVisibility(View.GONE);

		RadioGroup rg2 = (RadioGroup)findViewById(R.id.rgLoanPurposeOther);
		rg2.setVisibility(View.GONE);
	}

	public void refinancePropertySelected(View view){
		TextView tv1 = (TextView)findViewById(R.id.tvReason);
		tv1.setVisibility(View.VISIBLE);

		TextView tv2 = (TextView)findViewById(R.id.tvOtherReason);
		tv2.setVisibility(View.GONE);

		RadioGroup rg1 = (RadioGroup)findViewById(R.id.rgReason);
		rg1.setVisibility(View.VISIBLE);

		TextView tv3 = (TextView)findViewById(R.id.tvLoanPurposeOther);
		tv3.setVisibility(View.GONE);

		EditText et = (EditText)findViewById(R.id.etloanPurposeOther);
		et.setVisibility(View.GONE);

		RadioGroup rg2 = (RadioGroup)findViewById(R.id.rgLoanPurposeOther);
		rg2.setVisibility(View.GONE);
	}

	public void otherSelected(View view){
		TextView tv1 = (TextView)findViewById(R.id.tvReason);
		tv1.setVisibility(View.GONE);

		TextView tv2 = (TextView)findViewById(R.id.tvOtherReason);
		tv2.setVisibility(View.VISIBLE);

		RadioGroup rg1 = (RadioGroup)findViewById(R.id.rgReason);
		rg1.setVisibility(View.GONE);

		TextView tv3 = (TextView)findViewById(R.id.tvLoanPurposeOther);
		tv3.setVisibility(View.VISIBLE);

		EditText et = (EditText)findViewById(R.id.etloanPurposeOther);
		et.setVisibility(View.VISIBLE);

		RadioGroup rg2 = (RadioGroup)findViewById(R.id.rgLoanPurposeOther);
		rg2.setVisibility(View.VISIBLE);
	}

	public void principleInterestSelected(View view){
		LinearLayout ll = (LinearLayout)findViewById(R.id.llInterestOnlyPeriodSpecify);
		ll.setVisibility(View.GONE);
	}

	public void interestOnlyPeriodSelected(View view){
		LinearLayout ll = (LinearLayout)findViewById(R.id.llInterestOnlyPeriodSpecify);
		ll.setVisibility(View.VISIBLE);
	}
	
	
	// get customer resident postal code when loan settled
	public void getTotalLoanAmount() {

		etTotalLoanAmount = (EditText) findViewById(R.id.etTotalLoanAmount);
		vTotalLoanAmount = etTotalLoanAmount.getText().toString();
		methods md = new methods();
		md.editorSharePreferences(this, "dTotalLoanAmount",
				vTotalLoanAmount);
		
		System.out.println("get total loan amount " + md.retriveSharePreference(this, "vTotalLoanAmount", "dTotalLoanAmount"));
	}


	public void setTotalLoanAmount() {

		etTotalLoanAmount = (EditText) findViewById(R.id.etTotalLoanAmount);
		methods md = new methods();
		etTotalLoanAmount.setText(md.retriveSharePreference(this,
				"vTotalLoanAmount",
				"dTotalLoanAmount"));
		
		System.out.println("set total loan amount " + md.retriveSharePreference(this, "vTotalLoanAmount", "dTotalLoanAmount"));
	
	}
	
	
	// get value loan type
		public void getRadioBtnLoanType() {

			System.out.println("Test 121 ");
			// Gets a reference to our radio group
			// rgExistingCustomer is the name of our radio group
			rgLoanType = (RadioGroup) findViewById(R.id.rgLoanType);
			rgLoanType
					.setOnCheckedChangeListener(new OnCheckedChangeListener() {

						public void onCheckedChanged(RadioGroup group, int checkedId) {
							System.out.println("Test 122 ");
							methods md = new methods();
							variables vb = new variables();
							// Returns an integer which represents the selected
							// radio button's ID
							int selected = rgLoanType.getCheckedRadioButtonId();

							System.out.println(" get tested 1 " + selected);

							// Gets a reference to our "selected" radio button
							RadioButton loanTypeBtn = (RadioButton) findViewById(selected);
							// bring the selected button to variable class

							vLoanTypes = loanTypeBtn.getText()
									.toString();

							SharedPreferences preferences = getSharedPreferences(
									"Mb4Me", 0);
							SharedPreferences.Editor editor = preferences.edit();
							editor.putString("dLoanTypes", vLoanTypes);
							editor.putInt("dLoanTypesInt", selected);
							editor.commit();
							System.out.println(" get tested 1.2 " + selected);
						}

					});

		}
		
		
		// set value loan type
		public void setRadioBtnLoanType() {

			// Gets a reference to our radio group
			// rgExistingCustomer is the name of our radio group
			rgLoanType = (RadioGroup) findViewById(R.id.rgLoanType);

			methods md = new methods();
			variables vb = new variables();
			int selected = 0;
			// Returns an integer which represents the selected
			// radio button's ID
			// selected = residentStatus
			// .getCheckedRadioButtonId();

			System.out.println("tested1 " + selected);

			SharedPreferences preferences = getSharedPreferences("Mb4Me", 0);
			int retriveValue = md.retriveShareIntPreference(this, selected,
					"dLoanTypesInt");

			System.out.println("tested2.2 " + retriveValue);
			try {
			// sets a reference to our "selected" radio button
			RadioButton LoanTypesBtn = (RadioButton) findViewById(retriveValue);
			LoanTypesBtn.setChecked(true);
			System.out.println("dLoanTypes " + md.retriveSharePreference(this,vLoanTypes, "dLoanTypes" ));
			
			//check condition for yes or not
			if (md.retriveSharePreference(this,vLoanTypes, "dLoanTypes" ).equals("New")) {
				System.out.println("vLoanTypes " + vLoanTypes);
				//this is to visible the addition edit text
				LinearLayout ll1 = (LinearLayout)findViewById(R.id.llVariedAccountNumber);
				ll1.setVisibility(View.GONE);

				LinearLayout ll2 = (LinearLayout)findViewById(R.id.llChangeToCurrentLimit);
				ll2.setVisibility(View.GONE);
			} else {
				
				//this is to disable the additional edit text
				LinearLayout ll1 = (LinearLayout)findViewById(R.id.llVariedAccountNumber);
				ll1.setVisibility(View.VISIBLE);

				LinearLayout ll2 = (LinearLayout)findViewById(R.id.llChangeToCurrentLimit);
				ll2.setVisibility(View.VISIBLE);
			}
			} catch (NullPointerException ex ){
				System.out.print(" null 2 " );
			}
		}
		
		
		// get customer Varied Account No
		public void getVariedAccountNo() {

			etVariedAccountNo = (EditText) findViewById(R.id.etVariedAccountNo);
			vVariedAccountNo = etVariedAccountNo.getText().toString();
			methods md = new methods();
			md.editorSharePreferences(this, "dVariedAccountNo",
					vVariedAccountNo);
			
			System.out.println("get total loan amount " + md.retriveSharePreference(this, "vVariedAccountNo", "dVariedAccountNo"));
		}


		public void setVariedAccountNo() {

			etVariedAccountNo = (EditText) findViewById(R.id.etVariedAccountNo);
			methods md = new methods();
			etVariedAccountNo.setText(md.retriveSharePreference(this,
					"vVariedAccountNo",
					"dVariedAccountNo"));
			
			System.out.println("set total loan amount " + md.retriveSharePreference(this, "vVariedAccountNo", "dVariedAccountNo"));
		
		}
		
		public void getVariedAccountNoNull() {

			
			methods md = new methods();
			md.editorSharePreferences(this, "dVariedAccountNo",
					null);
		}
		
		
		// get customer Change To CurrentLimit
				public void getChangeToCurrentLimit() {

					etChangeToCurrentLimit = (EditText) findViewById(R.id.etChangeToCurrentLimit);
					vChangeToCurrentLimit= etChangeToCurrentLimit.getText().toString();
					methods md = new methods();
					md.editorSharePreferences(this, "dChangeToCurrentLimit",
							vChangeToCurrentLimit);
					
					System.out.println("get total loan amount " + md.retriveSharePreference(this, "vChangeToCurrentLimit", "dChangeToCurrentLimit"));
				}


				public void setChangeToCurrentLimit() {

					etChangeToCurrentLimit = (EditText) findViewById(R.id.etChangeToCurrentLimit);
					methods md = new methods();
					etChangeToCurrentLimit.setText(md.retriveSharePreference(this,
							"vChangeToCurrentLimit",
							"dChangeToCurrentLimit"));
					
					System.out.println("set total loan amount " + md.retriveSharePreference(this, "vChangeToCurrentLimit", "dChangeToCurrentLimit"));
				
				}
				
				public void getChangeToCurrentLimitNull() {

					
					methods md = new methods();
					md.editorSharePreferences(this, "dChangeToCurrentLimit",
							null);
				}
				
				
				
				// get customer Change To CurrentLimit
				public void getLoanLimitPerSplit1() {

					etLoanLimitPerSplit1 = (EditText) findViewById(R.id.etLoanLimitPerSplit);
					vLoanLimitPerSplit1= etLoanLimitPerSplit1.getText().toString();
					methods md = new methods();
					md.editorSharePreferences(this, "dLoanLimitPerSplit1",
							vLoanLimitPerSplit1);
					
					System.out.println("get total loan amount " + md.retriveSharePreference(this, "vLoanLimitPerSplit1", "dLoanLimitPerSplit1"));
				}


				public void setLoanLimitPerSplit1() {

					etLoanLimitPerSplit1= (EditText) findViewById(R.id.etLoanLimitPerSplit);
					methods md = new methods();
					etLoanLimitPerSplit1.setText(md.retriveSharePreference(this,
							"vLoanLimitPerSplit1",
							"dLoanLimitPerSplit1"));
					
					System.out.println("set total loan amount " + md.retriveSharePreference(this, "vLoanLimitPerSplit1", "dLoanLimitPerSplit1"));
				
				}
				
				public void getLoanLimitPerSplit1Null() {

					
					methods md = new methods();
					md.editorSharePreferences(this, "dLoanLimitPerSplit1",
							null);
				}
				
				
				
				// get value loan type
				public void getRadioBtnLoanPurpose1() {

					System.out.println("Test 121 ");
					// Gets a reference to our radio group
					// rgExistingCustomer is the name of our radio group
					rgLoanPurpose1 = (RadioGroup) findViewById(R.id.rgLoanPurpose);
					rgLoanPurpose1
							.setOnCheckedChangeListener(new OnCheckedChangeListener() {

								public void onCheckedChanged(RadioGroup group, int checkedId) {
									System.out.println("Test 122 ");
									methods md = new methods();
									variables vb = new variables();
									// Returns an integer which represents the selected
									// radio button's ID
									int selected = rgLoanPurpose1.getCheckedRadioButtonId();

									System.out.println(" get tested 1 " + selected);

									// Gets a reference to our "selected" radio button
									RadioButton LoanPurposeBtn = (RadioButton) findViewById(selected);
									// bring the selected button to variable class

									vLoanPurpose1 = LoanPurposeBtn.getText()
											.toString();

									SharedPreferences preferences = getSharedPreferences(
											"Mb4Me", 0);
									SharedPreferences.Editor editor = preferences.edit();
									editor.putString("dLoanPurpose", vLoanPurpose1);
									editor.putInt("dLoanPurposeInt", selected);
									editor.commit();
									System.out.println(" get tested 1.2 " + selected);
								}

							});

				}
				
				
				// set value loan type
				public void setLoanPurpose1() {

					// Gets a reference to our radio group
					// rgExistingCustomer is the name of our radio group
					rgLoanPurpose1 = (RadioGroup) findViewById(R.id.rgLoanType);

					methods md = new methods();
					variables vb = new variables();
					int selected = 0;
					// Returns an integer which represents the selected
					// radio button's ID
					// selected = residentStatus
					// .getCheckedRadioButtonId();

					System.out.println("tested1 " + selected);

					SharedPreferences preferences = getSharedPreferences("Mb4Me", 0);
					int retriveValue = md.retriveShareIntPreference(this, selected,
							"dLoanPurposeInt");

					System.out.println("tested2.2 " + retriveValue);
					try {
					// sets a reference to our "selected" radio button
					RadioButton LoanTypesBtn = (RadioButton) findViewById(retriveValue);
					LoanTypesBtn.setChecked(true);
					System.out.println("dLoanPurpose " + md.retriveSharePreference(this,vLoanPurpose, "dLoanPurpose" ));
					
					//check condition for yes or not
					
				}

}
